package cn.com.fuhuitong.main.mine.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.app.BaseApplication
import cn.com.fuhuitong.base.BaseViewModel
import cn.com.fuhuitong.http.BaseResponse
import cn.com.fuhuitong.http.HttpResponse
import cn.com.fuhuitong.http.ViewModelSubscribeListener
import cn.com.fuhuitong.main.access.entity.UserInfoEntity
import cn.com.fuhuitong.main.access.entity.UserResponse
import cn.com.fuhuitong.main.home.entity.CharityListResponse
import cn.com.fuhuitong.main.home.entity.HomeResponse
import cn.com.fuhuitong.main.mine.entity.*
import cn.com.fuhuitong.main.mine.mode.MineManager
import cn.com.fuhuitong.main.order.mode.OrderManager
import cn.com.fuhuitong.utils.JsonUtils
import com.blankj.utilcode.util.SPStaticUtils
import io.reactivex.disposables.Disposable

class MineViewModel(application: Application) : BaseViewModel(application) {


    private val mineManager: MineManager = MineManager()

    val orderSignDataLiveData = MutableLiveData<HttpResponse<OrderSignResponse>>()

    val userInfoDataLiveData = MutableLiveData<HttpResponse<UserResponse>>()


    /**
     * 个人中心订单数量标识
     */
    fun orderSign() {
        val listener =
            object : ViewModelSubscribeListener<OrderSignResponse>(orderSignDataLiveData) {}
        val orderSignDisposable: Disposable = mineManager.orderSign(
            BaseApplication.userToken(), listener
        )
        dispose.add(orderSignDisposable)
    }

    /**
     * 个人信息
     * */
    fun getUserInfo() {
        val listener =
            object : ViewModelSubscribeListener<UserResponse>(userInfoDataLiveData) {
                override fun onSuccess(resp: UserResponse) {
                    super.onSuccess(resp)
                    val userInfo: UserInfoEntity =
                        with(BaseApplication.userInfo() ?: resp.data) {
                            this.avatar = resp.data.avatar
                            this.nickname = resp.data.nickname
                            this.mobile = resp.data.mobile
                            this.score = resp.data.score
                            this.group_name = resp.data.group_name
                            this.group_id = resp.data.group_id
                            this
                        }
                    SPStaticUtils.put(
                        AppConstance.BUNDLE_USER_INFO,
                        JsonUtils.objectToString(userInfo)
                    )
                }
            }
        val getUserInfoDisposable: Disposable = mineManager.getUserInfo(
            BaseApplication.userToken(), listener
        )
        dispose.add(getUserInfoDisposable)
    }

}